import { AxiosRequestConfig } from 'axios'
import { CommonAxios } from './index'
let http = new CommonAxios({
	baseURL: '/api',
	timeout: 60000
})
export class HttpRequest {
	/**
	 * 统一封装get请求
	 * @params url 请求地址
	 * @params params 请求参数
	 * @params config 配置
	 **/

	static get(url: string, params?: any, config?: AxiosRequestConfig): Promise<any> {
		return new Promise((resolve, reject) => {
			http.instance
				.get(url, { params, ...config })
				.then(res => {
					resolve(res)
				})
				.catch(error => {
					reject(error)
				})
		})
	}

	/**
	 * 统一封装post请求
	 * @params url 请求地址
	 * @params params 请求参数
	 * @params config 配置
	 **/
	static post(url: string, data?: any, config?: AxiosRequestConfig): Promise<any> {
		return new Promise((reslove, reject) => {
			http.instance
				.post(url, data, config)
				.then(res => {
					reslove(res)
				})
				.catch(error => {
					reject(error)
				})
		})
	}

	/**
	 * 统一封装put请求
	 * @params url 请求地址
	 * @params params 请求参数
	 * @params config 配置
	 **/

	static put(url: string, data?: any, config?: AxiosRequestConfig): Promise<any> {
		return new Promise((resolve, reject) => {
			http.instance
				.put(url, data, config)
				.then(res => {
					resolve(res)
				})
				.catch(error => {
					reject(error)
				})
		})
	}

	/**
	 * 统一封装delete请求
	 * @params url 请求地址
	 * @params params 请求参数
	 * @params config 配置
	 **/

	static delete(url: string, params?: any, config?: AxiosRequestConfig): Promise<any> {
		return new Promise((reslove, reject) => {
			http.instance
				.delete(url, { params, ...config })
				.then(res => {
					reslove(res)
				})
				.catch(error => {
					reject(error)
				})
		})
	}
}
let service = new CommonAxios({
	baseURL: '/api',
	timeout: 60000
})
export default service
